//
//  ViewController2.m
//  SavaData
//
//  Created by student on 16/3/14.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController2.h"

@interface ViewController2 ()<UITabBarDelegate,UITableViewDataSource>
//学生数组
@property(nonatomic,strong)NSMutableArray *stuList;
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *idTextField;
@property (weak, nonatomic) IBOutlet UITextField *ageTextField;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ViewController2
/*重写getter方法，懒加载*/
-(NSMutableArray *)stuList
{
    if(_stuList == nil)
    {
        //判断存档是否存在，如果之前已有存档文件，则从存档中读取出来以前的数据
        NSFileManager *fm = [NSFileManager defaultManager];
        if([fm fileExistsAtPath:[self plistPath]])
        {
            _stuList = [NSMutableArray arrayWithContentsOfFile:[self plistPath]];
            
        }
        else
        {
            //没有存档文件则新建一个空数组
            _stuList = [NSMutableArray array];
        }
    }
    return _stuList;

}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
/*plist文件存储位置*/
-(NSString *)plistPath
{
    NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [docPath stringByAppendingPathComponent:@"students.plist"];
    
    return filePath;
}

/*保存数据到plist（覆盖以前的数据）*/
-(void)savePlist
{
    //写入到文件
    [self.stuList writeToFile:[self plistPath] atomically:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//增加学生
- (IBAction)add2BtnPressed:(id)sender
{
    //1.将信息放入一个字典
    NSMutableDictionary *dict = [@{@"stuId":self.idTextField.text,
                                   @"stuName":self.nameTextField.text,
                                   @"stuAge":self.ageTextField.text} mutableCopy];
    //2.将字典放入学生数组
    [self.stuList addObject:dict];
    //3.数组保存一下
    [self savePlist];
    
    //刷新表格
    [self.tableView reloadData];
    
}
//删除学生
- (IBAction)remove2:(id)sender {
//1.遍历是学生数组
    for(NSMutableDictionary *mDict in self.stuList)
    {
        //2.如果学号相同
        if([mDict[@"stuId"] isEqualToString:self.idTextField.text])
        {
            //3.将该学生从这个字典从数组中移除
            [self.stuList removeObject:mDict];
            break;
        }
    }
    //4.保存一下
    [self savePlist];
    //刷新表格
    [self.tableView reloadData];

    
}
//修改学生
- (IBAction)modify2:(id)sender {
    //1.遍历是学生数组
    for(NSMutableDictionary *mDict in self.stuList)
    {
        //2.如果学号相同
        if([mDict[@"stuId"] isEqualToString:self.idTextField.text])
        {
            //3.修改学生信息
            mDict[@"stuName"] = self.nameTextField.text;
            mDict[@"stuAge"] = self.ageTextField.text;
            break;
        }
    }
    //4.保存一下
    [self savePlist];
    
    //刷新表格
    [self.tableView reloadData];

}
//根据学号查询学生

- (IBAction)find2:(id)sender {
    //1.遍历是学生数组
    for(NSMutableDictionary *mDict in self.stuList)
    {
        //2.如果学号相同
        if([mDict[@"stuId"] isEqualToString:self.idTextField.text])
        {
            //3.将该学生从这个字典从数组中移除
            self.nameTextField.text = mDict[@"stuName"];
            self.ageTextField.text = mDict[@"stuAge"];
            break;
        }
    }
    
}
- (IBAction)findAll2:(id)sender {
    //1.遍历是学生数组
    for(NSMutableDictionary *mDict in self.stuList)
    {
        NSLog(@"%@ %@ %@",mDict[@"stuName"],mDict[@"stuId"],mDict[@"stuAge"]);
    }

  
}
#pragma mark - 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.stuList.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    
    UILabel *label1 = [cell.contentView viewWithTag:101];
    UILabel *label2 = [cell.contentView viewWithTag:102];
    UILabel *label3 = [cell.contentView viewWithTag:103];
    
    NSDictionary *dict = self.stuList[indexPath.row];
    label1.text = dict[@"stuId"];
    label2.text = dict[@"stuName"];
    label3.text = dict[@"stuAge"];
    return cell;
    
}

@end
